/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.tableTools;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;

/**
 *
 * @author isen
 */
public class Row extends javax.swing.JPanel {

    public static final int CONTENT_OK = 0;
    public static final int WRONG_DISTANCE = 1;
    public static final int WRONG_HAUTEUR = 2;
    public static final int WRONG_BOTH = WRONG_DISTANCE + WRONG_HAUTEUR;
    /**
     * Creates new form Row
     */
    public Row() {
        initComponents();
    }
    public Row(String distance, String hauteur) {
        initComponents();
        this.distance.setText(distance);
        this.hauteur.setText(hauteur);
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        line = new javax.swing.JPanel();
        hauteur = new javax.swing.JFormattedTextField(NumberFormat.INTEGER_FIELD);
        distance = new javax.swing.JFormattedTextField(NumberFormat.INTEGER_FIELD);

        line.setMaximumSize(new java.awt.Dimension(306, 20));
        line.setMinimumSize(new java.awt.Dimension(306, 20));

        hauteur.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        hauteur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hauteurActionPerformed(evt);
            }
        });
        hauteur.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                hauteurFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                hauteurFocusLost(evt);
            }
        });
        hauteur.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                hauteurKeyTyped(evt);
            }
        });

        distance.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        distance.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                distanceActionPerformed(evt);
            }
        });
        distance.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                distanceFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                distanceFocusLost(evt);
            }
        });

        javax.swing.GroupLayout lineLayout = new javax.swing.GroupLayout(line);
        line.setLayout(lineLayout);
        lineLayout.setHorizontalGroup(
            lineLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lineLayout.createSequentialGroup()
                .addComponent(distance, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(hauteur, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        lineLayout.setVerticalGroup(
            lineLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lineLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(hauteur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(distance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(line, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(line, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void distanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_distanceActionPerformed
       checkBG();
    }//GEN-LAST:event_distanceActionPerformed

    private void hauteurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hauteurActionPerformed
      checkBG();
    }//GEN-LAST:event_hauteurActionPerformed

    private void hauteurFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_hauteurFocusGained
        
        
    }//GEN-LAST:event_hauteurFocusGained

    private void hauteurKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_hauteurKeyTyped
        
    }//GEN-LAST:event_hauteurKeyTyped

    private void distanceFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_distanceFocusGained
       
        distance.setBackground(Color.WHITE);
    }//GEN-LAST:event_distanceFocusGained

    private void distanceFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_distanceFocusLost
       checkBG();
    }//GEN-LAST:event_distanceFocusLost

    private void hauteurFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_hauteurFocusLost
       checkBG();
    }//GEN-LAST:event_hauteurFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField distance;
    private javax.swing.JFormattedTextField hauteur;
    private javax.swing.JPanel line;
    // End of variables declaration//GEN-END:variables

    public String getDistance() {
        return distance.getText();
    }

    public String getHauteur() {
        return hauteur.getText();
    }

        public int getIntDistance() {
        return Integer.parseInt(distance.getText());
    }

    public int getIntHauteur() {
        return Integer.parseInt(hauteur.getText());
    }
    public void setDistanceBg(Color c) {
        this.distance.setBackground(c);
    }

    public void setHauteurBg(Color c) {
        this.hauteur.setBackground(c);
    }

public void checkBG(){
    
    Container c;
    c = this;
    for (int i=0;i<8;i++){
        c = c.getParent();
        
    }
    
    int returnValue = ((TableMaker)(c)).verifyRowContent(this.getLocation());
    if (returnValue == CONTENT_OK ){
        hauteur.setBackground(Color.WHITE);
        hauteur.setToolTipText("");
        distance.setBackground(Color.WHITE);
        distance.setToolTipText("");
    }else if (returnValue == WRONG_HAUTEUR ){
        hauteur.setBackground(Color.RED);
        hauteur.setToolTipText("Valeur incorrecte");
        distance.setBackground(Color.WHITE);
        distance.setToolTipText("");
    } else if (returnValue ==WRONG_DISTANCE){
        hauteur.setBackground(Color.WHITE);
        hauteur.setToolTipText("");
        distance.setBackground(Color.RED);
        distance.setToolTipText("Valeur incorrecte");
    }else if (returnValue ==WRONG_BOTH){
        hauteur.setBackground(Color.RED);
        hauteur.setToolTipText("Valeur incorrecte");
        distance.setBackground(Color.RED);
        distance.setToolTipText("Valeur incorrecte");
    }
}


}
